package com.lambada;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName DemoLambada
 * @Description TODO
 * @Author ZhangY
 * @Date 2020/1/21 15:09
 * @Version 1.0.0
 */
public class DemoLambada {

    public static void main(String[] args) {
//        test01();
//        stream01();
//        stream02();
//        stream03();
//        stream04();
//        stream05();
        stream06();
    }


    /**
     * @MethodName test01
     * @author ZhangY 2020/1/21 15:10
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 老式代码编写方式
     */
    public static void test01() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
        }
        System.out.println(sum);
    }

    /**
     * @MethodName stream01
     * @author ZhangY 2020/1/21 15:16
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 使用stream流
     */
    public static void stream01() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        int sum = list.stream().reduce(0, (acc, value) -> acc + value);
        //reduce()作用是从一组值中生成一个新的值
        System.out.println(sum);
    }

    /**
     * @MethodName stream02
     * @author ZhangY 2020/1/21 15:27
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 使用stream流
     */
    public static void stream02() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> result = list.stream().filter((value) -> value > 2).collect(Collectors.toList());
        //filter()作用过滤并获取符合条件的值
        result.forEach((value) -> System.out.print(value));
//        System.out.println(result);
    }

    /**
     * @MethodName stream03
     * @author ZhangY 2020/1/21 15:45
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 使用stream流
     */
    public static void stream03() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        List<String> result = list.stream().map(value -> String.format("String:%s", value)).collect(Collectors.toList());
        //map 函数的作用是将流中的一个值转换成一个新的值，举个例子，我们要将一个 List 转换成 List ，那么就可以使用 map 方法
        result.forEach(System.out::print);
    }

    /**
     * @MethodName stream04
     * @author ZhangY 2020/1/21 15:54
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 使用stream流
     */
    public static void stream04() {
        List<String> list = Arrays.asList("abc", "def", "ghi");
        List<Character> result = list.stream().flatMap(value -> {
            char[] chars = value.toCharArray();
            Character[] characters = new Character[chars.length];
            for(int i = 0; i < characters.length; i++){
                characters[i] = chars[i];
            }
            return Stream.of(characters);
            //flatMap作用将多个 Stream 连接成一个 Stream
        }).collect(Collectors.toList());
        result.forEach(System.out::print);
    }

    /**
     * @MethodName stream05
     * @author ZhangY 2020/1/21 16:04
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 使用stream流
     */
    public static void stream05() {
//        List<Integer> list = Arrays.asList(0, 1, 2, 3);
        List<String> list = Arrays.asList("1", "2222", "3333", "4");
        Comparator<String> comparator = (o1, o2) -> o1.compareTo(o2);
        System.out.println(list.stream().min(comparator).get());
        System.out.println(list.stream().max(comparator).get());
    }

    /**
     * @MethodName stream06
     * @author ZhangY 2020/1/21 16:10
     * @param []
     * @return void
     * @since 1.0.0
     * @Description 使用stream流
     */
    public static void stream06() {
        List<Integer> list = Arrays.asList(0, 1, 2, 3);
        int count = list.stream().reduce(0, (acc, item) -> acc + item).intValue();
        //reduce 函数的一个参数为循环的初始值，这里计算累加时初始值为 0，acc 代表已经计算的结果，item 表示循环的每个元素。
        System.out.println(count);
    }


    /**
      * @Description anyMatch/allMatch/noneMatch（匹配）
      *              anyMatch：Stream 中任意一个元素符合传入的 predicate，返回 true
      *              allMatch：Stream 中全部元素符合传入的 predicate，返回 true
      *              noneMatch：Stream 中没有一个元素符合传入的 predicate，返回 true
      * @author ZhangYue26
      * @date 2020/11/3
      * @param []
      * @return void
      **/
    private static void stream07() {
        Student s1 = new Student(1L, "肖战", 15, "浙江");
        Student s2 = new Student(2L, "王一博", 15, "湖北");
        Student s3 = new Student(3L, "杨紫", 17, "北京");
        Student s4 = new Student(4L, "李现", 17, "浙江");
        List<Student> students = new ArrayList();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        students.add(s4);
        Boolean anyMatch = students.stream().anyMatch(s ->"湖北".equals(s.getAddress()));
        if (anyMatch) {
            System.out.println("有湖北人");
        }
        Boolean allMatch = students.stream().allMatch(s -> s.getAge()>=15);
        if (allMatch) {
            System.out.println("所有学生都满15周岁");
        }
        Boolean noneMatch = students.stream().noneMatch(s -> "杨洋".equals(s.getName()));
        if (noneMatch) {
            System.out.println("没有叫杨洋的同学");
        }
    }

    /**
     * @ClassName Student
     * @Description 静态内部学生类
     * @Author ZhangY
     * @Date 2020/1/21 15:09
     * @Version 1.0.0
     */
    public static class Student {
        private Long id;
        private String name;
        private int age;
        private String address;
        public Student() {}
        public Student(Long id, String name, int age, String address) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.address = address;
        }
        @Override
        public String toString() {
            return "Student{" + "id=" + id + ", name='" + name + '\'' +
                    ", age=" + age + ", address='" + address + '\'' + '}';
        }
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student student = (Student) o;
            return age == student.age &&
                    Objects.equals(id, student.id) &&
                    Objects.equals(name, student.name) &&
                    Objects.equals(address, student.address);
        }
        @Override
        public int hashCode() {
            return Objects.hash(id, name, age, address);
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }
    }


}
